package com.hh.controller;

import com.hh.domain.ResponseResult;
import com.hh.entity.Login;
import com.hh.entity.Student;
import com.hh.entity.dto.LoginStudentDTO;
import com.hh.entity.vo.LoginModifyPasswordVO;
import com.hh.enums.AppHttpCodeEnum;
import com.hh.mapper.LoginMapper;
import com.hh.mapper.StudentMapper;
import com.hh.service.LoginService;
import com.hh.service.StudentService;
import com.hh.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
 * @Author huxy
 * 2022年10月16日
 * */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    LoginMapper loginMapper;
    @Autowired
    LoginService loginService;
    /*
     * 修改密码
     * @Params LoginModifyPasswordVO
     * @return 修改结果
     * */
    @PostMapping("/modifyPassword")
    public ResponseResult modifyPassword(@RequestBody LoginModifyPasswordVO loginModifyPassworddto){
        Integer result;
        result = loginService.modifyPassword(loginModifyPassworddto);
            if (result==0){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
            }else{
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"修改成功");
            }
    }

    /*
     * 添加用户
     * @Params LoginStudentDTO
     * @return 结果
     * */
    @RequestMapping("/addUser")
    public ResponseResult addUser(@RequestBody LoginStudentDTO loginStudentDTO){
        Student student = loginStudentDTO.getStudent(loginStudentDTO);
        Login login = loginStudentDTO.getLogin(loginStudentDTO);
        try {
            int logininsert = loginMapper.insert(login);
            int studentinsert = studentMapper.insert(student);
            if (studentinsert > 0) {
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), "添加成功");
            }
        }catch(Exception e) {
            return ResponseResult.okResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(), "添加失败,ID重复");
        }
        return ResponseResult.okResult();
    }
}
